# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 07-csv_案例.py
"""
import csv
url = 'https://danjuanapp.com/djapi/v3/filter/fund?type=2&order_by=2y&size=20&page=1'

import requests

def get_data():
    headers = {
        'user-agent':'asdasdasdasdas'
    }
    res =requests.get(url,headers=headers).json()
    items = res.get('data').get('items')
    data = []
    for item in items:
        fd_name  =item.get('fd_name')
        unit_nav = item.get('unit_nav')
        data.append([fd_name,unit_nav])

    with open('xialuo.csv','w',encoding='utf-8',newline='') as f:
        writer = csv.writer(f)
        writer.writerow(['title','price'])
        writer.writerows(data)

get_data()
